package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysCropSvc;
import com.kmxd.ams.client.system.dto.SysCropAddDTO;
import com.kmxd.ams.client.system.dto.SysCropEditDTO;
import com.kmxd.ams.client.system.dto.SysCropQueryDTO;
import com.kmxd.ams.client.system.vo.SysCropInitAddVO;
import com.kmxd.ams.client.system.vo.SysCropListVO;
import com.kmxd.ams.client.system.vo.SysCropVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 单位信息表 前端控制器
 *
 * @author ZuoLG
 * @since 2022-07-14
 */
@RestController
@Tag(name = "单位信息表")
@RequestMapping("/system/sysCrop")
@AllArgsConstructor
public class SysCropCtl {

  private ISysCropSvc sysCropSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysCropListVO>> selectPage(Page reqPage, SysCropQueryDTO req) {
    req.setParentId(
        ObjectUtil.isNotEmpty(req.getParentId()) && ObjectUtil.notEqual(req.getParentId(), 0L)
            ? req.getParentId()
            : SecurityUtil.getCropId());
    return R.success(sysCropSvc.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @GetMapping("/selectList")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "列表查询")
  public R<List<SysCropListVO>> selectList(SysCropQueryDTO req) {
    return R.success(sysCropSvc.selectList(req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysCrop:add")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysCropAddDTO req) {
    sysCropSvc.add(req);
    return R.success();
  }

  /** 校验 */
  @PostMapping("/checkCrop")
  @AutoLog(module = "单位信息校验", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "校验")
  public R<Void> checkCrop(@Validated @RequestBody SysCropAddDTO req) {
    sysCropSvc.checkCrop(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysCrop:edit")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysCropEditDTO req) {
    sysCropSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysCrop:remove")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH_RISK, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysCropSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysCropVO> view(@RequestParam Long id) {
    return R.success(sysCropSvc.view(id));
  }

  /** 初始化添加 */
  @GetMapping("/initAdd")
  @SaCheckPermission("system:sysCrop:initAdd")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "初始化添加")
  public R<SysCropInitAddVO> initAdd() {
    return R.success(sysCropSvc.initAdd());
  }

  /** 查询单位树 */
  @GetMapping("/listCropTree")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询单位树")
  public R<List<Tree<String>>> listCropTree() {
    return R.success(sysCropSvc.listCropTree());
  }

  /** 查询下拉单位树，排除当前节点以及子节点 */
  @GetMapping("/selectCropTreeAndExcludeNode")
  @AutoLog(
      module = "单位管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询下拉单位树，排除当前节点以及子节点")
  public R<List<Tree<String>>> selectTreeAndExcludeNode(
      @RequestParam(required = false) String nodeId) {
    return R.success(sysCropSvc.selectCropTreeAndExcludeNode(nodeId));
  }

  /** 查询完整单位 */
  @GetMapping("/selectCropTreeAll")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询完整单位")
  public R<List<Tree<String>>> selectCropTreeAll() {
    return R.success(sysCropSvc.selectCropTreeAll());
  }

  /** 查询下拉单位树 */
  @GetMapping("/selectCropTree")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询下拉单位树")
  public R<List<Tree<String>>> selectTree() {
    return R.success(sysCropSvc.selectCropTree());
  }

  /** 查询下拉单位树 */
  @GetMapping("/allParentList")
  @AutoLog(module = "单位管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询下拉单位树")
  public R<List<SysCropListVO>> allParent() {
    return R.success(sysCropSvc.findAllParent(SecurityUtil.getCropId()));
  }
}
